package com.linchuansi.juc;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * *
 *
 * @author wb
 * @date 2025-11-22 22:18*@since 1.0.0
 */
public class CyclicBarrierDemo {
    static ExecutorService executorService= Executors.newFixedThreadPool(10);
     static CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
    public static void main(String[] args) {
        await();
    }
    public  static void await()
    {
        executorService.submit(()->{
            try {
                System.out.println("开始执行任务一");
                Thread.sleep(1000);
                cyclicBarrier.await();
                System.out.println("任务一结束");

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            System.out.println("开始执行任务二");
            try {
                cyclicBarrier.await();
                Thread.sleep(2000);
                System.out.println("任务二结束");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executorService.shutdown();
    }
}
